package com.rabbiter.fm.service.impl;

import com.rabbiter.fm.dao.DataAnalysisDao;
import com.rabbiter.fm.model.ProductCategoryRanking;
import com.rabbiter.fm.service.DataAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DataAnalysisServiceImpl implements DataAnalysisService {

    @Autowired
    private DataAnalysisDao dataAnalysisDao;

    /**
     * 获取数据分析内容，数据从数据库中获取
     * @return 返回数据分析的结果
     */
    @Override
    public Map<String, Object> getDataAnalysis() {
        Map<String, Object> responseData = new HashMap<>();

        // 获取商品种类的统计数据
        List<Integer> stats = dataAnalysisDao.getProductCategoryStats();
        responseData.put("stats", stats);

        // 获取商品种类的销售数据
        List<String> name1 = dataAnalysisDao.getProductCategorySalesNames();
        List<Integer> sales = dataAnalysisDao.getProductCategorySales();
        Map<String, Object> barChart1 = new HashMap<>();
        barChart1.put("xData", name1);
        barChart1.put("seriesData", sales);

        // 获取商品种类的访问量数据
        List<String> names2 = dataAnalysisDao.getProductCategoryVisitsNames();
        List<Integer> visits = dataAnalysisDao.getProductCategoryVisits();
        Map<String, Object> barChart2 = new HashMap<>();
        barChart2.put("xData", names2);
        barChart2.put("seriesData", visits);

        // 构建商品种类的排名数据（包含交易额）
        List<ProductCategoryRanking> ranks = dataAnalysisDao.getProductCategoryRankings();
        List<ProductCategoryRanking> rankingsData = new ArrayList<>();
        for (ProductCategoryRanking ranking : ranks) {
            ProductCategoryRanking rankingItem = new ProductCategoryRanking();
            rankingItem.setName(ranking.getName());
            rankingItem.setTotalSales(ranking.getTotalSales());
            rankingsData.add(rankingItem);
        }

        // 将数据加入响应
        responseData.put("barChart1", barChart1);
        responseData.put("barChart2", barChart2);
        responseData.put("rankings", rankingsData);

        return responseData;
    }
}
